﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Collections;
using System.Net.Sockets;
using System.Net;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using ClientEndpoint;
using Package;
using ClientEndpoint;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private TcpClient klient = null;
        BinaryReader czytanie = null;
        BinaryWriter pisanie = null;
        private IPAddress CloudAddress = IPAddress.Parse("127.0.0.1");
        private string nazwa = "";
        private int CloudPort = 50001;
        private bool PolaczenieAktywne = false;
        delegate void SetTextCallBack(string tekst);
        delegate void SetScrollCallBack();
        ArrayList ports = new ArrayList();
        private List<string> config = new List<string>();
        private int portTCPCloudReceive;
        private int portTCPAgentReceive;
        private int portReceived;
        string[] args = Environment.GetCommandLineArgs();
        
        public Form1()
        {
            InitializeComponent();
           
            
        }
        //konfiguracja z pliku
        public void getConfig()
        {
            string Konfiguracja = "";
            StreamReader objReader = new StreamReader(args[1]);
            while (Konfiguracja != null)
            {
            Konfiguracja = objReader.ReadLine();
            if (Konfiguracja != null)
            config.Add(Konfiguracja);
            }
            objReader.Close();
            nazwa = config[0];

            portTCPCloudReceive = Int32.Parse(config[1]);
            portTCPAgentReceive = Int32.Parse(config[2]);

            for(int i = 3; i<=5; i++)
            {

            ports.Add(Int32.Parse(config[i]));
            }
        }

        public void establishConWithClaud()
        {
            try
            {
                ClientProperties.getInstance(nazwa, ports, HelloPackage.NodeType.NNI);
                var cCend = CableCloudEndPoint.getInstance(CloudAddress, CloudPort);
                int portReceived = CableCloudEndPoint.getInstance().mainPort;
                listBox1.Items.Add(portReceived);
            }
            catch
            {
                MessageBox.Show("Chmura nie podlaczona");
            }
        }

        private void SetText(string tekst)
        {
            if (listBox1.InvokeRequired)
            {
                SetTextCallBack f = new SetTextCallBack(SetText);
                this.Invoke(f, new object[] { tekst });
            }
            else
            {
                this.listBox1.Items.Add(tekst);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            listBox1.Items.Add(args[0]);

        }

        private void button2_Click(object sender, EventArgs e)
        {
            getConfig();
            establishConWithClaud();
        }

       //listBox1.Items.Add(args[0]);

        

       
       

        
    }
}
